package com.awn.weidian.demo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.awn.weidian.demo.mapper.AreaMenu;
import com.awn.weidian.demo.pojo.Areamenu;

/**
 * 这是一个地区的类，提供了全国地区的信息
 * @author "liyi"
 *
 */
@CrossOrigin
@RestController
@RequestMapping("/api/")
public class AreaController{
	
	@Autowired
	AreaMenu ism;
	
	/**
	 * 返回所有的地区树形结构
	 * @return
	 */
	@GetMapping("/getList")
    public List<Map<String,Object>> getList(){
		
		List<Map<String,Object>> cList= new ArrayList<Map<String,Object>>();
		
		/**
		 * 得到所有的省，市，县
		 */
		List<Areamenu>  shenglist = ism.getShengList();
		List<Areamenu>  shilist = ism.getShiList();
		List<Areamenu>  xianlist = ism.getXianList();
		
    	for(int i = 0;i < shenglist.size();i++) {
            LinkedHashMap<String, Object> prMap = new LinkedHashMap<String, Object>();
            prMap.put("label", shenglist.get(i).getRname());
            prMap.put("id", shenglist.get(i).getRid());
            int isl = shenglist.get(i).getRid(); 
			List<LinkedHashMap<String, Object>> citylist = new ArrayList<LinkedHashMap<String, Object>>();
			for(int j = 0;j <shilist.size();j++) {
				if(shilist.get(j).getFather() == isl) {
					LinkedHashMap<String, Object> cityMap = new LinkedHashMap<String, Object>();
					cityMap.put("label", shilist.get(j).getRname());
					cityMap.put("id", shilist.get(j).getRid());
					int jsl = shilist.get(j).getRid();
					List<Map<String, Object>> shilist1 = new ArrayList<Map<String, Object>>();
					for (int k = 0; k < xianlist.size(); k++) {
						if (xianlist.get(k).getFather() == jsl) {
							LinkedHashMap<String, Object> xianMap = new LinkedHashMap<String, Object>();
							xianMap.put("label",xianlist.get(k).getRname());
							xianMap.put("id",xianlist.get(k).getRid());
							shilist1.add(xianMap);
							cityMap.put("children",shilist1);
						}
					}
					
					citylist.add(cityMap);
				}
			}
			prMap.put("children", citylist);
            cList.add(prMap);
    	}
    	
    	return cList;
    }
	
	/**
	 * 返回所有的省
	 * @return
	 */
	@GetMapping("/getShengList")
	public List<Areamenu> getShengList(){
		return ism.getShengList();
	}
	
	/**
	 * 返回所有的市
	 * @return
	 */
	@GetMapping("/getShiList")
	public List<Areamenu> getShiList(){
		return ism.getShiList();
	}
	
	/**
	 * 返回所有的县
	 * @return
	 */
	@GetMapping("/getXianList")
	public List<Areamenu> getXianList(){
		return ism.getXianList();
	}
	
	/**
	 * 利用递归的方法返回指定的list<map>格式
	 * @return
	 */
	@GetMapping("/getListM")
	public List<Map<String, Object>> getListM(){
			List<Areamenu>  shenglist = ism.getObjectMapList();
			return findRows(0,shenglist);

	  }
	
	/**
	 * 
	 * @param id 父节点
	 * @param tt 总数据
	 * @return 返回是否存在子节点
	 */
	 public boolean isCheck(int id,List<Areamenu> tt){
			boolean ist = false;
			for(int i = 0 ;i < tt.size();i++){
				if(id == tt.get(i).getFather()){
					ist = true;
					break;
				}
			}
			return ist;
	  }
	 
	 /**
	  * 
	  * @param id 父节点
	  * @param dt 总数据
	  * @return 返回总数据
	  */
	 public List<Map<String, Object>> findRows(int id,List<Areamenu> dt){
			List<Map<String,Object>> clist= new ArrayList<Map<String,Object>>();
			for(int i = 0 ;i < dt.size();i++){	
				if(id == dt.get(i).getFather()){
					Map<String, Object> map = new HashMap<String, Object>();	
					map.put("label", dt.get(i).getRname());
					if(isCheck(dt.get(i).getRid(),dt)){
						map.put("children",findRows(dt.get(i).getRid(),dt)) ;
					}
					clist.add(map);
				}
			}  
			return clist;		
		}


}
